package com.mdz.sbgl.Controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mdz.sbgl.Entity.Gsxxb;
import com.mdz.sbgl.Entity.Sbxxb;
import com.mdz.sbgl.Entity.Wenhb;
import com.mdz.sbgl.Entity.Whflb;
import com.mdz.sbgl.Mapper.WenhbMapper;
import com.mdz.sbgl.Mapper.WhflbMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 孟德志
 * @since 2021-01-27
 */
@Controller
@RequestMapping("/sbgl/wenhb")
public class WenhbController {

    @Value("${gsid}")
    private String gsid;

    @Autowired
    private WenhbMapper wenhbMapper;

    @Autowired
    private HttpSession session;

    @ResponseBody
    @RequestMapping(value = "/getlist/{miaos}/{gsid}", method = RequestMethod.GET)
    public List<Wenhb> getlist(@PathVariable("miaos") String miaos,@PathVariable("gsid") String gsid) {
        System.out.println("这里gsid的值："+gsid);
        List<Wenhb> wenhbList = wenhbMapper.selectList(new QueryWrapper<Wenhb>().eq("miaos",miaos).eq("gsid",gsid));
        return wenhbList;
    }

    @ResponseBody
    @RequestMapping(value = "/getwenhb/{gsid}", method = RequestMethod.GET)
    public List<Wenhb> getwenhb(@PathVariable("gsid") String gsid) {
        List<Wenhb> wenhbs = wenhbMapper.selectList(new QueryWrapper<Wenhb>().eq("gsid",gsid));
        return wenhbs;
    }

    @ResponseBody
    @RequestMapping(value = "/insertfl", method = RequestMethod.POST)
    public int insertfl(Wenhb wenhb) {
        return wenhbMapper.insert(wenhb);
    }

    @ResponseBody
    @RequestMapping(value = "/getwenhblist",method = RequestMethod.GET)
    public String getwenhblist(HttpServletResponse response, @RequestParam(value = "pageNumber",defaultValue = "1") int pageNumber, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize, @RequestParam(value = "cxtj",required = false,defaultValue = "") String cxtj, @RequestParam(value = "gsid",required = false,defaultValue = "") String gsid){
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        PageHelper.startPage(pageNumber, pageSize);
//        List<Wenhb> ncList=wenhbMapper.selectList(new QueryWrapper<Wenhb>().like("gsid",gsid).and(wrapper -> wrapper.like("whmc",cxtj).or().like("wenh",cxtj).or().like("miaos",cxtj)));
        String userid=session.getAttribute("userid").toString();
        List<Wenhb> ncList=new ArrayList<>();
        if (userid.equals("1")){
            ncList=wenhbMapper.selectList(new QueryWrapper<Wenhb>().like("gsid",cxtj).or().like("whmc",cxtj).or().like("miaos",cxtj).or().like("wenh",cxtj));
        }else {
            System.out.println(userid);
            ncList=wenhbMapper.selectList(new QueryWrapper<Wenhb>().eq("gsid",gsid));
        }
        PageInfo page=new PageInfo(ncList);
        List<Sbxxb> rows = page.getList();
        int total = (int)page.getTotal();
        JSONObject result = new JSONObject();
        result.put("rows",rows);
        result.put("total",total);
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    public Integer update(Wenhb wenhb, @PathVariable("id") String id) {
        return wenhbMapper.update(wenhb, new UpdateWrapper<Wenhb>().eq("id", id));
    }

    @ResponseBody
    @RequestMapping(value = "/updatebyid/{ids}", method = RequestMethod.PUT)
    public Integer updatebyid(Wenhb wenhb, @PathVariable("ids") String ids) {
        return wenhbMapper.updatebyid(wenhb.getWhmc(),wenhb.getWenh(),ids);
    }

    @PostMapping("/remove/{id}")
    @ResponseBody
    public Integer remove(@PathVariable("id") String id)
    {
        return wenhbMapper.delete(new QueryWrapper<Wenhb>().eq("id",id));
    }

    @GetMapping("/getGsid")
    @ResponseBody
    public String getGsid(){
        return gsid;
    }

}

